from flask import Flask, request, jsonify
import ddddocr

app = Flask(__name__)

# 初始化OCR
ocr = ddddocr.DdddOcr()

@app.route('/ocr', methods=['POST'])
def ocr_endpoint():
    try:
        # 获取上传的图片数据
        img_data = request.files['image'].read()
        
        # 调用OCR识别图片
        result = ocr.classification(img_data)
        
        # 返回识别结果
        return jsonify({'text': result})
    except Exception as e:
        # 发生错误时返回错误信息
        return jsonify({'error': str(e)}), 400

if __name__ == '__main__':
    app.run(host='0.0.0.0',debug=True)